<%@ taglib prefix="g" uri="/web-app/WEB-INF/tld/grails.tld" %>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <meta name="layout" content="main" />
        <title>Show GriaJob</title>   
        <g:javascript library="prototype" /> 
        <resource:tabView /> 
        <resource:accordion skin="default" />      
        <resource:tooltip />
        
    </head>
    <body>
        <div class="nav">
            <span class="menuButton"><a class="home" href="${createLinkTo(dir:'')}">Home</a></span>
            <span class="menuButton"><g:link class="list" action="list">GriaJob List</g:link></span>
            <span class="menuButton"><g:link class="create" action="create">New GriaJob</g:link></span>
        </div>
        <div class="body">
            <h1>Show GriaJob</h1>
            <g:if test="${flash.message}">
            <div class="message">${flash.message}</div>
            </g:if>
            <richui:tabView id="tabView">
                <richui:tabLabels>
                    <richui:tabLabel selected="true" title="Info" />
                    <richui:tabLabel title="Options" />
                    <richui:tabLabel title="Inputs" />
                    <richui:tabLabel title="Outputs" />
                </richui:tabLabels>
                <richui:tabContents>
                <richui:tabContent>
                    <div class="dialog">
                        <table>
                            <tbody>
                                <tr class="prop">
                                 <td valign="top" class="name">Id:</td>
                                 <td valign="top" class="value">${fieldValue(bean:griaJob, field:'id')}</td>
                                </tr>      
                                <tr class="prop">
                                  <td valign="top" class="name">Application:</td>
                                  <td valign="top" class="value"><g:link controller="griaApplication" action="show" id="${griaJob?.application?.id}">${griaJob?.application?.encodeAsHTML()}</g:link></td>
                                </tr>
                                <tr class="prop">
                                 <td valign="top" class="name">Owner:</td>    
                                 <td valign="top" class="value"><g:link controller="user" action="show" id="${griaJob?.owner?.id}">${griaJob?.owner?.encodeAsHTML()}</g:link></td>
                                </tr>           
                                <tr class="prop">
                                 <td valign="top" class="name">Readers:</td>     
                                 <td  valign="top" style="text-align:left;" class="value">
                                    <ul>
                                       <g:each var="r" in="${griaJob.readers}">
                                         <li><g:link controller="user" action="show" id="${r.id}">${r?.encodeAsHTML()}</g:link></li>
                                       </g:each>
                                    </ul>
                                 </td>       
                                </tr>        
                               <tr class="prop">
                                <td valign="top" class="name">Resource ID:</td>                      
                                <td valign="top" class="value">${fieldValue(bean:griaJob, field:'resourceID')}</td>    
                               </tr>               
                               <tr class="prop">
                                <td valign="top" class="name">Status:</td>       
                                <td valign="top" class="value">${fieldValue(bean:griaJob, field:'status')}</td>           
                               </tr>    
                            </tbody>
                        </table>
                   </div>
                  </richui:tabContent>
                <richui:tabContent> 
                       <div id="argumentSaveDiv" ></div>
                       <g:formRemote name="argumentForm" update="argumentSaveDiv"
                                        url="[action: 'updateArgs']">    
                      <input type="hidden" name="jobid" value="${griaJob.id}" /> 
                      <table>
                       <tbody>
                        <g:each var="argument" in="${griaJob.application.arguments}">
	                        <tr class="prop">
	                          <td>
	                               <g:link title="${argument.tooltip}" controller="applicationArgument" action="show" id="${argument.id}">${argument}</g:link>
	                               <richui:tooltip id="${argument.id}" />
	                          </td>
	                          <td>
	                               <g:set var="argValue" value="${argument.defaultValue}"/>
	                               <g:if test="${!griaJob.arguments.isEmpty()}">
	                                   <g:each var="arg" in="${griaJob.arguments}">
                                          <g:if test="${arg.type.name.equalsIgnoreCase(argument.name)}">   
                                              <g:set var="argValue" value="${arg.value}"/>                                                                   
                                          </g:if> 
                                       </g:each>                                   
                                   </g:if>
                                   <g:if test="${argument.type == 'boolean'}">     
	                                   <g:checkBox name="${argument.name}" value="${argValue}" />
	                               </g:if>
	                               <g:if test="${argument.type == 'int'}">
                                       <g:textField name="${argument.name}" value="${argValue}" />
                                   </g:if>
	                               <g:if test="${argument.type == 'double'}">
                                       <g:textField name="${argument.name}" value="${argValue}" />
                                   </g:if>
                                   <g:if test="${argument.type == 'enum'}">
                                       <g:textField name="${argument.name}" value="${argValue}" />
                                   </g:if>
	                          </td>
                            </tr>
                        </g:each>
                      </tbody>
                     </table> 
                     <g:submitButton type="submit" name="update" value="Save" />
                     </g:formRemote>           
                  </richui:tabContent>
                <richui:tabContent>
                  <div class="dialog">
                        <g:set var="counter" value="${1}" /> 
                        <g:each var="r" in="${griaJob.application.inputs}">
                            <g:set var="found" value="${false}" />    
                            <g:set var="tabID">${"tab" + counter}</g:set>
                            <g:set var="divID">${"div" + counter}</g:set>
                            <h1>${r}</h1>   
                            <table>
                             <tbody>  
                                <tr class="prop">
                                    <td valign="top" class="name"> Selected File: </td>
                                    <td valign="top" class="name">
                                    <g:if test="${!griaJob.inputs.isEmpty()}">
                                          <g:each var="input" in="${griaJob.inputs}">
                                            <g:if test="${input.type.type == r}">                                               
                                                <div id='${divID}' ><g:link controller="griaFile" action="show" id="${input.id}">${input}</g:link></div>                               
                                                <g:set var="found" value="${true}" /> 
                                            </g:if> 
                                          </g:each>                                   
                                    </g:if>
                                    <g:if test="${!found}">
                                         None
                                    </g:if>
                                    </td>
                                </tr> 
                                <tr class="prop">
                                <td  colspan="3" valign="top" class="name">
                                <richui:tabView id = '${tabID}'>
                                 <richui:tabLabels> 
                                    <richui:tabLabel selected="true" title="Select Existing" />
                                    <richui:tabLabel title="Create New" />
                                  </richui:tabLabels>
                                <richui:tabContents>
                                  <richui:tabContent>
                                        <g:formRemote name="myForm" update='${divID}' url="[action: 'updateFile']">
                                            <input type="hidden" name="jobid" value="${griaJob.id}" />
                                            <g:select optionKey="id" from="${gria.GriaFile.findWhere(type:gria.FileType.findWhere(type:r))}" name="fileid" value="${griaJob?.inputs?.id}" ></g:select>
                                            <g:submitButton name="update" value="Select" />
                                        </g:formRemote>
                                  </richui:tabContent>  
                                  <richui:tabContent>
                                    <g:form action="upload" method="post" enctype="multipart/form-data" >
                                         <input type="hidden" name="type" value="${r}" />
                                      <input type="hidden" name="jobid" value="${griaJob.id}" />
                                       <table>
                                            <tbody>             
                                           <tr class="prop">
                                               <td valign="top" class="name">
                                                   <label for="description">Name:</label>
                                                </td>
                                             <td valign="top" class="name">
                                                  <input type="text" id="description" name="description" value=""/>
                                              </td>
                                          </tr>
                                          <tr class="prop">
                                              <td valign="top" class="name">
                                                   <label for="datFile">File:</label>
                                               </td>
                                               <td valign="top">
                                                   <input type="file" name="file"/>
                                               </td>
                                           </tr>              
                                        </tbody>
                                       </table>            
                                      <span class="button"><input class="save" type="submit" value="Upload" /></span> 
                                       </g:form>
                                  </richui:tabContent>   
                                  </richui:tabContents>
                                  </richui:tabView>
                                                 
                                </td>
                                </tr>    
                             </tbody>
                            </table>  
                        <g:set var="counter" value="${counter + 1}" /><br>                      
                        </g:each>
                   </div>
                  </richui:tabContent>
                <richui:tabContent>
                  <div class="dialog">
                        <g:set var="counter" value="${1}" /> 
                        <g:each var="r" in="${griaJob.application.outputs}">    
                            <g:set var="tabID">${"otab" + counter}</g:set>
                            <g:set var="divID">${"odiv" + counter}</g:set>
                            <h1>${r}</h1>   
                            <table>
                             <tbody>                              
                                    <!-- 
                                    if the file is ready on the job conv then want to be able to
                                    1. download it
                                    2. Save it to new stager
                                     -->                               
                                    <g:if test="${!griaJob.outputs.isEmpty()}">
                                          <g:each var="output" in="${griaJob.outputs}">
                                            <g:if test="${output.type = gria.FileType.findWhere(type:r)}">
                                                <tr class="prop" >
                                                    <td>File Name: <i> "${output.fileName}" </i></td>                                        
                                                </tr>
                                                <tr>
                                                <td class="prop" valign="top">
                                                <g:form controller="griaFile" >
                                                    <input type="hidden" name="id" value="${output.id}" />
                                                    <span class="button"><g:actionSubmit class="download" value="Download" /></span>
                                                    <span class="button"><g:actionSubmit class="copy" action="copy" value="Save" /></span>
                                                </g:form>  
                                                </td>
                                                </tr>                     
                                            </g:if>                    
                                          </g:each>                                   
                                    </g:if>                             
                             </tbody>
                            </table>  
                        <g:set var="counter" value="${counter + 1}" /><br>                      
                        </g:each>
                   </div>
                </richui:tabContent> 
                </richui:tabContents>
            </richui:tabView>
          
            <div class="buttons">
                <g:form>
                    <input type="hidden" name="id" value="${griaJob?.id}" />
                <!--     <span class="button"><g:actionSubmit class="edit" value="Edit" /></span> -->
                    <span class="button"><g:actionSubmit class="delete" onclick="return confirm('Are you sure?');" value="Delete" /></span>
                    <span class="button"><g:actionSubmit action="submitJob" class="edit" value="Submit" /></span>
                    <span class="button"><g:actionSubmit action="updateStatus" class="update" value="Update Status" /></span>
                    <span class="button"><g:actionSubmit action="viewLog" class="update" value="View Log" /></span>
                </g:form>
            </div>
        </div>
    </body>
</html>
